﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using BlogEngine.Core;

using Resources;

using Page = System.Web.UI.Page;
using App_Code;

public partial class photo : BlogEngine.Core.Web.Controls.BlogBasePage
{
    public string[] img;
    public string[] name;
    public string[] fullname;
    public int count = 0;
    protected override void OnInit(EventArgs e)
    {
        btnUploadImage.Click += BtnUploadImageClick;
        base.OnInit(e);
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        //test_truyen_bien.Text = "day la bien truyen qua";//
        var delete = this.Request.QueryString["delete"];

        try
        {
            var path = Utils.RelativeWebRoot;

            var relativeFolder = "galary" + Path.DirectorySeparatorChar;
            var folder = string.Format("{0}files{1}", BlogSettings.Instance.StorageLocation, Path.DirectorySeparatorChar);

            if (delete != null)
            {
                File.Delete(Server.MapPath(folder + relativeFolder) + delete);
                Page.Response.Redirect(Utils.AbsoluteWebRoot+"photo.aspx", true);
            }
            DirectoryInfo dir = new DirectoryInfo(Server.MapPath(folder + relativeFolder));
            
            FileInfo[] file = dir.GetFiles("*.*");

            count = file.Length;
            img = new string[count + 3];
            name = new string[count + 3];
            fullname = new string[count + 3];
            for (int i = 0; i < file.Length; i++)
            {
                string a = Path.GetFileName(file[i].Name);

                img[i] = string.Format(
                  "<a href='{0}image.axd?picture={1}'><img height='300px' src=\"{0}image.axd?picture={1}\" alt=\"\" /></a>",
                  path,
                  Server.UrlEncode(relativeFolder.Replace("\\", "/") + a));
                name[i] = a.Substring(0, a.IndexOf('.'));
                fullname[i] = a;
            }


        }
        catch (Exception fas)
        {
        }
       
    }

    private static string SizeFormat(float size, string formatString)
    {
        if (size < 1024)
        {
            return string.Format("{0} bytes", size.ToString(formatString));
        }

        if (size < Math.Pow(1024, 2))
        {
            return string.Format("{0} kb", (size / 1024).ToString(formatString));
        }

        if (size < Math.Pow(1024, 3))
        {
            return string.Format("{0} mb", (size / Math.Pow(1024, 2)).ToString(formatString));
        }

        if (size < Math.Pow(1024, 4))
        {
            return string.Format("{0} gb", (size / Math.Pow(1024, 3)).ToString(formatString));
        }

        return size.ToString(formatString);
    }

    private void Upload(string virtualFolder, FileUpload control, string fileName)
    {
        var folder = Server.MapPath(virtualFolder);
        if (!Directory.Exists(folder))
        {
            Directory.CreateDirectory(folder);
        }

        control.PostedFile.SaveAs(folder + fileName);
    }

    protected void BtnUploadImageClick(object sender, EventArgs e)
    {
        var relativeFolder = "galary" + Path.DirectorySeparatorChar;
        var folder = string.Format("{0}files{1}", BlogSettings.Instance.StorageLocation, Path.DirectorySeparatorChar);
        string fileName = txtUploadImage.FileName;
        if (fileName != "")
        {
            Upload(folder + relativeFolder, txtUploadImage, fileName);
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }
    }
}
